const db = require('../db/index');

// 添加岗位
exports.addPosition = (req, res) => {
	// 查询重复名称
	const sql = 'select * from position where position_name=? and is_delete=0';
	db.query(sql, req.body.position_name, (err, results) => {
		if (err) return res.cc(err);
		if (results.length !== 0) return res.cc('岗位名称重复，请重新输入');
		const sql = 'insert into position set ?';
		db.query(sql, req.body, (err, results) => {
			if (err) return res.cc(err);
			res.cc('添加成功', 0)
		})
	})
}

// 删除岗位
exports.deletePosition = (req, res) => {
	// 判断当前岗位是否有用户
	const sql = 'select * from user where id_position=?';
	db.query(sql, req.body.position_id, (err, results) => {
		if (err) return res.cc(err);
		if (results.length !== 0) return res.cc('当前岗位下存在员工，无法删除');
		// 删除岗位
		const sql = 'update position set is_delete=1 where position_id=?';
		db.query(sql, req.body.position_id, (err, results) => {
			if (err) return res.cc(err);
			res.cc('删除成功', 0);
		})
	})
}

// 修改岗位
exports.updatePosition = (req, res) => {
	// 查询重复名称
	const sql = 'select * from position where position_name=? and is_delete=0';
	db.query(sql, req.body.position_name, (err, results) => {
		if (err) return res.cc(err);
		if (results.length !== 0) return res.cc('岗位名称重复，请重新输入');
		const sql = 'update position set ? where position_id=?';
		db.query(sql, [req.body, req.body.position_id], (err, results) => {
			if (err) return res.cc(err);
			res.cc('修改成功', 0);
		})
	})
}

// 启用禁用岗位
exports.startPosition = (req, res) => {
	const sql = 'update position set ? where position_id=?';
	db.query(sql, [req.body, req.body.position_id], (err, results) => {
		if (err) return res.cc(err);
		res.cc('启用禁用修改成功', 0);
	})
}

// 选择框查询岗位
exports.getPosition = (req, res) => {
	const sql = 'select * from position where is_delete=0 and start=0';
	db.query(sql, (err, results) => {
		if (err) return res.cc(err);
		let arr = results.map((item) => {
			return Object.assign(
				{},
				{ position_id: item.position_id },
				{ posttion_name: item.position_name }
			)
		})
		if(arr.length === 0) { return res.cc('当前没有岗位')}
		res.send({
			status: 0,
			message: '查询成功',
			data: arr
		})
	});
}

// 分页查询岗位列表
exports.getPositionList = (req, res) => {
	// 获取岗位总数
	const sql = 'select count(*) from position where is_delete=0';
	db.query(sql, (err, results) => {
		if (err) return res.cc(err);
		const listNumber = results[0]['count(*)'];
		if (listNumber === 0) return res.cc('当前没有数据');
		const sql = 'select * from position where is_delete=0';
		db.query(sql, (err, results) => {
			if (err) return res.cc(err);
			let arr = results.map((item) => {
				return Object.assign(
					{},
					{ position_id: item.position_id },
					{ position_name: item.position_name },
					{ user_id: item.user_id },
					{ add_date: item.add_date },
					{ start: item.start }
				)
			})
			// 将创建人id替换为创建人名字
			const sql = 'select * from user';
			db.query(sql, (err, results) => {
				if (err) return res.cc(err);
				for(let i = 0; i < arr.length ; i++) {
					for(let j = 0; j < results.length ; j++) {
						if(arr[i].user_id === results[j].id){
							arr[i].user_id = results[j].nick_name
						}
					}
				}
				// 提取 arr 里 position_id 的值并组成新数组
				const ids = arr.map(x => { return x.position_id });
				// 查出所有用户数据并从数组中，筛选个数
				const sql = 'select * from user where state < 2';
				db.query(sql, (err, results) => {
					if (err) return res.cc(err);
					for(let i = 0; i < ids.length ; i++){
						let n = 0;
						for(let j = 0; j < results.length ; j++){
							if (results[j].id_position === ids[i]){
								n++;
							}
						}
						Object.defineProperty(arr[i], 'user_number', {
							value: n,
							writable: true,
							enumerable: true,
							configurable: true
						})
					}
					res.send({
						status: 0,
						message: '查询成功',
						data: arr,
						listNumber
					})
				})
			})
		})
	})
}